package com.example.startscreen;

import android.app.Activity;
import android.os.Bundle;
import java.util.Random;

import com.example.photo.MySurfaceView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;

public class BitmapPositioningActivity extends Activity implements
		SensorEventListener {

	// za KOMPAS

	private SensorManager sensorManager;

	private TextView txtRawData;
	private TextView txtDirection;
	private float myAzimuth = 0;
	private float myPitch = 0;
	private float myRoll = 0;
	//

	MySurfaceView mySurfaceView;
	// int i = 1;
	// int j = 1;
	public static Bitmap b;

	// Bitmap b2 = b.create

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		b = BitmapFactory.decodeResource(getResources(),
				R.drawable.luvr_2000);

		Bitmap useThisBitmap = Bitmap.createScaledBitmap(b, 2000, 1600, true);

		b = useThisBitmap;

		// mySurfaceView = new MySurfaceView(this);
		// setContentView(mySurfaceView);
		
		
		setContentView(R.layout.compass);
		
		
		mySurfaceView = (MySurfaceView) findViewById(R.id.mySurfaceView);

		// za KOMPAS

		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		// add listener. The listener will be HelloAndroid (this) class
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_NORMAL);

		
		
		
		txtRawData = (TextView) findViewById(R.id.txt_info);
		txtDirection = (TextView) findViewById(R.id.txt_direction);
		txtRawData.setText("Compass");
		txtDirection.setText("");

	}

	// ZA KOMPAS

	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

	public void onSensorChanged(SensorEvent event) {

		// check sensor type
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

			// assign directions
			float x = event.values[0];
			float y = event.values[1];
			float z = event.values[2];

			// xCoor.setText("X: " + x);
			// yCoor.setText("Y: " + y);
			// zCoor.setText("Z: " + z);
		}

		// za KOMPAS

		myAzimuth = Math.round(event.values[0]);
		myPitch = Math.round(event.values[1]);
		myRoll = Math.round(event.values[2]);

		String out = String.format(
				"Azimuth: %.2f\n\nPitch:%.2f\n\nRoll:%.2f\n\n", myAzimuth,
				myPitch, myRoll);
		txtRawData.setText(out);

		printDirection();

		// za prosledjivanje parametara mysurfaceView-u
		mySurfaceView.horyzontalCalibration = (int) myAzimuth;
		mySurfaceView.verticalCalibration = (int) myPitch;
		if (mySurfaceView.verticalCalibration < 0) {
			mySurfaceView.verticalCalibration = mySurfaceView.verticalCalibration
					* (-1);

			// replace calibration WORKING YUPI ... CHANGING PLUS AND MINUS
			mySurfaceView.offset = 90 - mySurfaceView.verticalCalibration;
			mySurfaceView.verticalCalibration = 90 + mySurfaceView.offset;
			//
		}

		// PROMJENA DIJELA SLIKE

		// d = tableLayout.getBackground();

		// Bitmap bm = ((BitmapDrawable) d).getBitmap();
		// int width = bm.getWidth();
		// int height = bm.getHeight();
		// float scaleWidth = ((float) width * 2) / width;
		// float scaleHeight = ((float) height * 2) / height;
		// // create a matrix for the manipulation
		// Matrix matrix = new Matrix();
		// // resize the bit map
		// matrix.postScale(scaleWidth, scaleHeight);
		// // recreate the new Bitmap
		// Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
		// matrix, false);
		// BitmapDrawable result = new BitmapDrawable(resizedBitmap);
		// tableLayout.setBackgroundDrawable(result);
		// //tableLayout).setScaleType(ImageView.ScaleType.FIT_CENTER);
		// //tableLayout.setAdjustViewBounds(true);

	}

	private void printDirection() {

		if (myAzimuth < 22)
			txtDirection.setText("N");
		else if (myAzimuth >= 22 && myAzimuth < 67)
			txtDirection.setText("NE");
		else if (myAzimuth >= 67 && myAzimuth < 112)
			txtDirection.setText("E");
		else if (myAzimuth >= 112 && myAzimuth < 157)
			txtDirection.setText("SE");
		else if (myAzimuth >= 157 && myAzimuth < 202)
			txtDirection.setText("S");
		else if (myAzimuth >= 202 && myAzimuth < 247)
			txtDirection.setText("SW");
		else if (myAzimuth >= 247 && myAzimuth < 292)
			txtDirection.setText("W");
		else if (myAzimuth >= 292 && myAzimuth < 337)
			txtDirection.setText("NW");
		else if (myAzimuth >= 337)
			txtDirection.setText("N");
		else
			txtDirection.setText("");

	}

	// KOMPAS END

	@Override
	protected void onResume() {
		super.onResume();
		mySurfaceView.onResumeMySurfaceView();
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
				SensorManager.SENSOR_DELAY_NORMAL);
	}

	@Override
	protected void onPause() {
		super.onPause();
		mySurfaceView.onPauseMySurfaceView();
		sensorManager.unregisterListener(this);
	}

	// public class MySurfaceView extends SurfaceView implements Runnable {
	// Thread thread = null;
	// SurfaceHolder surfaceHolder;
	// volatile boolean running = false;
	// private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
	// Random random;
	//
	// public MySurfaceView(Context context) {
	// super(context);
	// surfaceHolder = getHolder();
	// random = new Random();
	// }
	//
	// public void onResumeMySurfaceView() {
	// running = true;
	// thread = new Thread(this);
	// thread.start();
	// }
	//
	// public void onPauseMySurfaceView() {
	// boolean retry = true;
	// running = false;
	// while (retry) {
	// try {
	// thread.join();
	// retry = false;
	// } catch (InterruptedException e) {
	// e.printStackTrace();
	// }
	// }
	// }
	//
	// @Override
	// public void run() {
	// // TODO Auto-generated method stub
	// while (running) {
	// if (surfaceHolder.getSurface().isValid()) {
	// // Canvas canvas = surfaceHolder.lockCanvas();
	// // //... actual drawing on canvas
	// // paint.setStyle(Paint.Style.STROKE);
	// // paint.setStrokeWidth(3);
	// // int w = canvas.getWidth();
	// // int h = canvas.getHeight();
	// // int x = random.nextInt(w - 1);
	// // int y = random.nextInt(h - 1);
	// // int r = random.nextInt(255);
	// // int g = random.nextInt(255);
	// // int b = random.nextInt(255);
	// // paint.setColor(0xff000000 + (r << 16) + (g << 8) + b);
	// // canvas.drawPoint(x, y, paint);
	// // surfaceHolder.unlockCanvasAndPost(canvas);
	//
	//
	//
	// Canvas canvas = surfaceHolder.lockCanvas();
	//
	// // int width = bm.getWidth();
	// // int height = bm.getHeight();
	// // float scaleWidth = ((float) width * 2) / width;
	// // float scaleHeight = ((float) height * 2) / height;
	// // // create a matrix for the manipulation
	// // Matrix matrix = new Matrix();
	// // // resize the bit map
	// // matrix.postScale(scaleWidth, scaleHeight);
	// // // recreate the new Bitmap
	// //Bitmap resizedBitmap = b.createScaledBitmap(b, i, j, true);
	// Bitmap resizedBitmap = b.createBitmap(b, 0, i, 400, 480);
	// i++;
	// j++;
	// if (i+480 >= b.getHeight()/* || j>=b.getWidth()*/) {
	// i = 1;
	// j = 1;
	// }
	// //BitmapDrawable result = new BitmapDrawable(resizedBitmap);
	//
	//
	// canvas.drawBitmap(resizedBitmap, 0, 0, null);
	// surfaceHolder.unlockCanvasAndPost(canvas);
	//
	// }
	// }
	// }
	// }

}